<template>
  <Dialog
    v-model="show"
    title="订单详情"
    width="800px"
    @close="close"
  >
    <div v-if="orderData" class="order-detail">
      <el-descriptions title="订单信息" :column="2" border>
        <el-descriptions-item label="订单编号">{{ orderData.orderNumber }}</el-descriptions-item>
        <el-descriptions-item label="下单时间">{{ orderData.orderTime }}</el-descriptions-item>
        <el-descriptions-item label="订单状态">
          <el-tag :type="getStatusType(orderData.status)">
            {{ getStatusText(orderData.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="支付方式">{{ orderData.paymentMethod }}</el-descriptions-item>
      </el-descriptions>
      
      <el-divider content-position="left">商家信息</el-divider>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="商家ID">{{ orderData.sellerId }}</el-descriptions-item>
        <el-descriptions-item label="商家名称">{{ orderData.sellerName }}</el-descriptions-item>
        <el-descriptions-item label="商家地址">{{ orderData.sellerAddress }}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{ orderData.sellerContact }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ orderData.sellerPhone }}</el-descriptions-item>
      </el-descriptions>
      
      <el-divider content-position="left">用户信息</el-divider>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="用户姓名">{{ orderData.userName }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ orderData.userPhone }}</el-descriptions-item>
        <el-descriptions-item label="邮箱地址">{{ orderData.userEmail }}</el-descriptions-item>
        <el-descriptions-item label="配送地址">{{ orderData.deliveryAddress }}</el-descriptions-item>
      </el-descriptions>
      
      <el-divider content-position="left">商品信息</el-divider>
      <el-table :data="orderData.items" style="width: 100%">
        <el-table-column label="商品图片" width="100">
          <template #default="scope">
            <div class="detail-dish-img" :style="{backgroundImage: `url(${scope.row.image})`}"></div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" />
        <el-table-column prop="price" label="单价" width="100">
          <template #default="scope">¥{{ scope.row.price }}</template>
        </el-table-column>
        <el-table-column prop="quantity" label="数量" width="100" />
        <el-table-column label="小计" width="120">
          <template #default="scope">
            ¥{{ (scope.row.price * scope.row.quantity).toFixed(2) }}
          </template>
        </el-table-column>
      </el-table>
      
      <el-divider content-position="left">金额信息</el-divider>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="商品原价">¥{{ orderData.originalPrice }}</el-descriptions-item>
        <el-descriptions-item label="打包费">¥{{ orderData.packagingFee }}</el-descriptions-item>
        <el-descriptions-item label="配送费">¥{{ orderData.deliveryFee }}</el-descriptions-item>
        <el-descriptions-item label="优惠券抵扣">-¥{{ orderData.couponDiscount }}</el-descriptions-item>
        <el-descriptions-item label="积分抵扣">-¥{{ orderData.pointsDiscount }}</el-descriptions-item>
        <el-descriptions-item label="实付金额" class="total-amount">¥{{ orderData.totalAmount }}</el-descriptions-item>
      </el-descriptions>
      
      <el-divider content-position="left">配送信息</el-divider>
      <el-descriptions v-if="orderData.deliveryStaff" :column="2" border>
        <el-descriptions-item label="配送员">{{ orderData.deliveryStaff.name }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ orderData.deliveryStaff.phone }}</el-descriptions-item>
        <el-descriptions-item v-if="orderData.deliveryTime" label="发货时间">
          {{ orderData.deliveryTime }}
        </el-descriptions-item>
        <el-descriptions-item v-if="orderData.receivingTime" label="收货时间">
          {{ orderData.receivingTime }}
        </el-descriptions-item>
      </el-descriptions>
      <el-empty v-else description="暂无配送信息"></el-empty>
      
      <!-- 评价信息区域 -->
      <template v-if="orderData.status === 'completed' && orderData.reviewContent">
        <el-divider content-position="left">评价信息</el-divider>
        <div class="review-info detail-info">
          <p><strong>评价时间:</strong> {{ orderData.reviewTime }}</p>
          <p><strong>评价内容:</strong> {{ orderData.reviewContent }}</p>
        </div>
      </template>
    </div>
  </Dialog>
</template>

<script setup>
import { ref, computed, watch } from "vue";
import Dialog from "@/components/dialog.vue";

const show = defineModel();
const emit = defineEmits(["success"]);

const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
});

// 处理订单数据
const orderData = computed(() => {
  return props.data && Object.keys(props.data).length > 0 ? props.data : null;
});

// 监听数据变化
watch(() => props.data, (newVal) => {
  console.log('订单详情数据:', newVal);
}, { deep: true });

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    'pending_payment': '待付款',
    'pending_delivery': '待发货',
    'pending_receipt': '待收货',
    'pending_review': '待评价',
    'completed': '已完成',
    'refund': '退款售后',
    'cancelled': '已取消'
  };
  return statusMap[status] || '未知状态';
};

// 获取状态标签类型
const getStatusType = (status) => {
  const typeMap = {
    'pending_payment': 'warning',
    'pending_delivery': 'primary',
    'pending_receipt': 'success',
    'pending_review': 'info',
    'completed': 'success',
    'refund': 'danger',
    'cancelled': 'info'
  };
  return typeMap[status] || '';
};

const close = () => {
  show.value = false;
};
</script>

<style lang="scss" scoped>
.h-500 {
  height: 500px;
}
.lht-38 {
  line-height: 38px;
}
.w-120 {
  width: 120px;
}
.mt24 {
  margin-top: 24px;
}
.detail-info {
  padding: 20px;
  background: #f5f7fa;
  border-radius: 4px;
}

.order-detail {
  padding: 10px;
  
  .el-descriptions {
    margin-bottom: 15px;
  }
}

.detail-dish-img {
  width: 60px;
  height: 60px;
  background-size: cover;
  background-position: center;
  border-radius: 4px;
}

.total-amount {
  :deep(.el-descriptions-item__content) {
    color: #f56c6c;
    font-weight: bold;
  }
}

.review-info {
  p {
    margin: 8px 0;
  }
}
</style>
